﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Configuration;
using System.Web;
using System.Runtime.Caching;

namespace Ruandao.HostServer
{
    using Ruandao.Misc;

    public class SiteWare
    {
        public Site Site { get; set; }

        public Ware Ware { get; set; }

        public string AliasName { get; set; }

        public string AccountContextName { get; set; }

        public string SpecialDomain { get; set; }

        public DateTime CreateDate { get; set; }

        private string connectionString = "";
        /// <summary>
        /// 
        /// </summary>
        public string ConnectionString 
        {
            get { return connectionString == "" ? this.Site.ConnectionString : this.connectionString; }
            set { this.connectionString = value; } 
        }

        private string providerName = "";
        /// <summary>
        /// 
        /// </summary>
        public string ProviderName 
        { 
            get { return providerName == "" ? this.Site.ProviderName : this.providerName; }
            set { this.providerName = value; } 
        }

        /// <summary>
        /// 在网站存储文件夹下的网件实例专属的子文件夹的名字，若为空则默认为网件的名字。
        /// </summary>
        public string WareFolder { get; set; }

        public string AccountContextCode { get; set; }

        public string RoleCategory { get; set; }

        public bool Allow { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string StoragePath
        {
            get
            {
                string root = Site.StorageLocation;

                return Ruandao.Misc.UtilityFunction.CombinePath(Site.StorageLocation, Ware.WareName + " Files");
            }
        }

        /// <summary>
        /// 网件存储位置的特殊映射。
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string SiteWareMapPath(string url)
        {
            if (SiteWare.Current.Ware.IsDynamicPath(url))
            {
                string AppRoot = VirtualPathUtility.ToAbsolute("~/");
                string path = WebUtility.ToClientAbsoluteUrl(url).Substring(AppRoot.Length-1);
                return StoragePath + path;
            }

            // 不满足上述条件，表示url不对应网件的动态虚拟目录这时使用IIS默认的路径映射。
            return HttpContext.Current.Request.MapPath(url);
        }


        static public SiteWare Current
        {
            get { return HostServerContext.Instance.GetCurrentSiteWare(); }
        }
    }
}
